package com.ruoyi.constant;


import com.ruoyi.common.Constants;

import java.math.BigDecimal;
import java.util.Arrays;
import java.util.List;
import java.util.Random;
import java.util.UUID;


/**
 * 随机数相关
 */
public class K_RandomUtil {

    public static String getUUID(){
        UUID uuid = UUID.randomUUID();
        return uuid.toString();
    }

    /**
     * 获取随机字符串，优化版的UUID
     * @return
     */
    public static String getRandStr(){
        StringBuilder result = new StringBuilder();
        String s1 = generateAssignLengthRandomCode(8, 4);
        String s2 = generateAssignLengthRandomCode(4, 1);
        String s3 = generateAssignLengthRandomCode(4, 4);
        String systemTime = K_DateUtil.getSystemDate("yyyy-MM-dd~HH:mm:ss");
        result.append(s1).append("-").append(s2).append("-").append(s3).append("-").append(systemTime);
        return result.toString();
    }

    /**
     * 生成指定位数包含数字和字母组合随机数
     * @param length (参数为字符个数)
     * @return
     */
    public static String generateAssignLengthRandomCode(int length) {
        String t = generateAssignLengthRandomCode(length,4);
        return t;
    }

    /**
     *
     * @param length 要取的长度
     * @param type 要取的类型
     *             1：只有数字 2：只有小写字母 3：只有大写字母 4：小写+数字 5：大写+数字
     *             默认为 4
     * @return
     */
    public static String generateAssignLengthRandomCode(int length,int type) {
        String str = "";
        if(type==1){
            str = getRandStr(Constants.FIGURE,length);
        }else if (type==2){
            str = getRandStr(Constants.LETTER,length);
        }else if (type==3){
            str = getRandStr(Constants.CAPITAL_LETTER,length);
        }else if (type==4){
            str = getRandStr(Constants.LETTER_AND_FIGURE,length);
        }else if (type==5){
            str = getRandStr(Constants.CAPITAL_LETTER_AND_FIGURE,length);
        }else {
            str = getRandStr(Constants.LETTER_AND_FIGURE,length);
        }
        return str;
    }

    private static String getRandStr(String str,int length){
        List<String> list = Arrays.asList(str.split(","));
        int randomIndex = list.size();
        Random rm = new Random();
        StringBuilder result = new StringBuilder();
        for (int i = 0; i < length; i++) {
            result.append(list.get(rm.nextInt(randomIndex)));
        }
        return result.toString();
    }

    /**
     * 生成指定范围的随机数
     * @return
     */
    public static int getRandomByNum(int start,int end){
        int port = (new Random().nextInt(end-start)+start);
        return port;
    }

    /**
     * 获取一个99.往上的率
     */
    public static BigDecimal getRate(){
        int num2 = getRandomByNum(0,99);
        BigDecimal num = new BigDecimal("99."+num2);
        return num;
    }




}
